package com.lrkj.web.phone;

import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.utils.ip.IpUtils;
import com.lrkj.framework.security.MiniContextUtils;
import com.lrkj.payfee.service.IBalanceRefillService;
import com.lrkj.payfee.vo.PayBalanceRefillVo;
import com.lrkj.weixin.entity.vo.PayInfoVo;
import com.lrkj.weixin.entity.vo.WxBackInfoVo;
import com.lrkj.weixin.service.impl.IWxPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author huxy
 * @version V1.0
 * @创建时间
 * @备注 微信支付
 * @类名
 * @节点
 */
@RequestMapping("/phone/wx")
@RestController
public class WxPayController extends CommonUseBaseController {
    @Autowired
    private IBalanceRefillService balanceRefillService;

    @Autowired
    private IWxPaymentService wxPayService;

    /**
     * @author huxy
     * @创建时间 2022/8/29 10:47 AM
     * @备注
     * @节点
     */
    @PostMapping("downOrder")
    public AjaxResult placeOrder(@RequestBody PayInfoVo payInfoVo, HttpServletRequest request) {
        payInfoVo.setIp(IpUtils.getIpAddr(request));
        payInfoVo.setOrderBy(MiniContextUtils.getOpenId());
        return wxPayService.placeOrder(payInfoVo);

    }

    @RequestMapping("msg")
    @ResponseBody
    public String callBack(@RequestBody String xmlData) {
        WxBackInfoVo wxBackInfoVo = wxPayService.payBack(xmlData);
        if (wxBackInfoVo.isOk()) {
            PayBalanceRefillVo payBalanceRefillVo = new PayBalanceRefillVo();
            payBalanceRefillVo.setParameterList(wxBackInfoVo.getParameterList());
            payBalanceRefillVo.setOrderNo(wxBackInfoVo.getWxOrderRecord().getOrderId());
            payBalanceRefillVo.setReceiptsMoney(wxBackInfoVo.getWxOrderRecord().getOrderPrice());
            payBalanceRefillVo.setIndex(wxBackInfoVo.getWxOrderRecord().getOrderType());
            payBalanceRefillVo.setPayeeTime(wxBackInfoVo.getWxOrderRecord().getOrderTime());
            payBalanceRefillVo.setStatus("10002");
            payBalanceRefillVo.setPayeeType("wx");
            payBalanceRefillVo.setOrderIntroduce(wxBackInfoVo.getWxOrderRecord().getOrderIntroduce());
            balanceRefillService.addBalanceRefillByWx(payBalanceRefillVo);
        }
        return wxBackInfoVo.getWxBack();
    }
}
